import collections
import requests
import re
import sqlite3
from bs4 import BeautifulSoup
from html.parser import HTMLParser
import os

def getDuplicates(posts):
    return [item for item, count in collections.Counter(posts).items() if count > 1]


def cleanhtml(raw_html):
    cleanr = re.compile("<.*?>")
    cleantext = re.sub(cleanr, "", raw_html)
    return cleantext


def listPosts(thread):
    t = 0
    for i in thread["posts"]:
        if t % 2 == 0:
            print(bcolors.WARNING + cleanhtml(i["com"]) + bcolors.ENDC)
        else:
            print(bcolors.OKGREEN + cleanhtml(i["com"]) + bcolors.ENDC)
        t += 1


def downloadImages(thread):
    pass


def saveToDatabase(thread):
    thread.save()


def findDuplicates(thread):
    pass


def saveRawDump(thread):
    pass


class bcolors:
    HEADER = "\033[95m"
    OKBLUE = "\033[94m"
    OKGREEN = "\033[92m"
    WARNING = "\033[93m"
    FAIL = "\033[91m"
    ENDC = "\033[0m"
    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"


class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.strict = False
        self.convert_charrefs = True
        self.fed = []

    def handle_data(self, d):
        self.fed.append(d)

    def get_data(self):
        return "".join(self.fed)


def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()


def loadBoardNames():

    conn = sqlite3.connect(os.path.expanduser('~/.meme_magic_stats/database.db'))
    retar = []
    c = conn.cursor()
    sql = """select boardname from posts group by boardname;"""
    
    values = c.execute(sql)
    for value in values: 
        retar.append(value[0])

    conn.close()
    
    return retar

        

